// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2023 HUAWEI TECHNOLOGIES CO., LTD.
 */
#include <linux/mm.h>
#include <asm/e820/api.h>

void vpmem_update_memory_range(void)
{
	int i;

	for (i = 0; i < e820_table->nr_entries; i++) {
		struct e820_entry *entry = &e820_table->entries[i];

		if (entry->type != E820_TYPE_VPMEM)
			continue;
		vpmem_add_memory_range(entry->addr >> PAGE_SHIFT,
			(entry->addr + entry->size - 1) >> PAGE_SHIFT);
	}
}

u64 e820__mapped_find(u64 *start, enum e820_type type)
{
	int i;

	for (i = 0; i < e820_table->nr_entries; i++) {
		struct e820_entry *entry = &e820_table->entries[i];

		if (type && entry->type != type)
			continue;

		if (start && entry->addr >= *start) {
			*start = entry->addr;
			return entry->size;
		}
	}
	return 0;
}

u64 vpmem__mapped_find(u64 *start, enum e820_type type)
{
	int i;

	for (i = 0; i < e820_table->nr_entries; i++) {
		struct e820_entry *entry = &e820_table->entries[i];

		if (type && entry->type != type)
			continue;

		if (start && entry->addr >= *start) {
			*start = entry->addr;
			return entry->size;
		}
	}
	return 0;
}
EXPORT_SYMBOL_GPL(vpmem__mapped_find);
